import  cloneDeep  from 'lodash/cloneDeep'
/**
 * 将提交值按照 dto 对象进行赋值
 * @param valueData 原始数据
 * @param dtoData 需要提交的数据对象
 */
export const dataDto=(valueData:any,dtoData:any) => { 
 const _dto = cloneDeep(dtoData)
 for (const key in _dto) {
   if (valueData.hasOwnProperty(key)) {
     _dto[key] = valueData[key]
   }
   
 }
 return _dto
} 






/**
 * 查找数组对象的某个下标
 * @param {Array} ary 查找的数组
 * @param {Functon} fn 判断的方法
 */
export const findIndex = <T = Recordable>(ary: Array<T>, fn: Fn): number => {
  if (ary.findIndex) {
    return ary.findIndex(fn);
  }
  let index = -1;
  ary.some((item: T, i: number, ary: Array<T>) => {
    const ret: T = fn(item, i, ary);
    if (ret) {
      index = i;
      return ret;
    }
  });
  return index;
};
// 返回对象中有值的字段
export const returnDataObj = (data: any) => {
  const _params: any = {};
  if (!isEmptyObj(data)) {
    for (const key in data) {
      if (
        data[key] !== null &&
        data[key] !== "" &&
        JSON.stringify(data[key]) !== "[]" &&
        JSON.stringify(data[key]) !== "{}"
      ) {
        _params[key] = data[key];
      }
    }
  }
  return _params;
};
/**
 * 数据非空验证
 * @desc 验证数据是否为 null undefined [] {} ''
 * @param data
 * @return {boolean}
 */
export const isEmpty = (data: any) => {
  if (data === null) return true;
  if (data === undefined) return true;
  if (Object.prototype.toString.call(data) === "[object Array]")
    return data.length === 0;
  if (Object.prototype.toString.call(data) === "[object Object]")
    return Object.keys(data).length === 0;
  if (typeof data === "string") return data.trim() === "";
  return false;
};
// 判断对象是否为空
export const isEmptyObj = (obj: any) => {
  return Reflect.ownKeys(obj).length === 0 && obj.constructor === Object;
};
/**
 * 有数据的 给对象赋值
 * obj 要赋值的对象
 * data 现有对象数据
 * */
export const copyDataToObj = (oldObj: any, data: any) => {
  const _obj = Object.assign({}, oldObj);
  if (!isEmptyObj(_obj)) {
    for (const key in _obj) {
      if (
        data[key] &&
        data[key] !== null &&
        data[key] !== "" &&
        typeof data[key] !== "object"
      ) {
        _obj[key] = data[key];
      }
    }
  }
  return _obj;
};

/**
 * 给对象所有key赋值
 * obj 要赋值的对象
 * data 现有对象数据
 * */
export const copyTotalDataToObj = (oldObj: any, data: any) => {
  const _obj = Object.assign({}, oldObj);
  if (!isEmptyObj(_obj)) {
    for (const key in _obj) {
      _obj[key] = data[key];
    }
  }
  return _obj;
};

// 省市区数据处理
// export const  provincCityFormatData = (data: any[]): ProvinceDataItem[] => {
//   return data.map(item => {
//     const children = item.children ? provincCityFormatData(item.children) : undefined;
//     return {
//       value: item.code,
//       label: item.name,
//       children,
//     };
//   });
// };
